<template>
	<div>
		<!-- 面包屑导航 -->
		<el-breadcrumb class="el-breadcrumb" separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>商品管理</el-breadcrumb-item>
			<el-breadcrumb-item>商品分类</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 卡片视图 -->
		<el-card>
			<!-- 添加分类按钮区域 -->
			<div class="addGoodCateBtnDiv">
				<el-row>
					<el-col :span="6">
						<el-button @click="addGoodCateBtnClick" type="primary">添加分类</el-button>
					</el-col>
				</el-row>
				<!-- 普通表格标签写法 -->
<!-- 				<el-row>
					<el-col :span="24">
						<el-table border stripe :data="goodsCateList">
							<el-table-column type="index"></el-table-column>
							<el-table-column label="分类名称" prop="cat_name"></el-table-column>
							<el-table-column label="是否有效" prop="cat_pid"></el-table-column>
							<el-table-column label="排序" prop="cat_level"></el-table-column>
							<el-table-column label="操作"></el-table-column>
						</el-table>
					</el-col>
				</el-row> -->
				<tree-table class="tree-table" :show-row-hover="false" border index-text="#" show-index :expand-type="false" :selection-type="false" :data="goodsCateList" :columns="columns">
					<template slot="isok" slot-scope="scope">
						<i style="color: lightgreem;" v-if="scope.row.cat_deleted === false" class="el-icon-success"></i>
						<i style="color: red;" v-else class="el-icon-error"></i>
					</template>
					<template slot="order" slot-scope="scope">
						<el-tag v-if="scope.row.cat_level===0" size="mini">一级</el-tag>
						<el-tag v-else-if="scope.row.cat_level===1" size="mini" type="success">二级</el-tag>
						<el-tag v-else-if="scope.row.cat_level===2" size="mini" type="warning">三级</el-tag>
					</template>
					<template slot="opt">
						<el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
						<el-button size="mini" type="primary" icon="el-icon-delete">删除</el-button>
					</template>
				</tree-table>
				<!-- 跳转视图 -->
				<div class="block">
					<el-pagination class="el-pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5, 10, 15, 20]" :page-size="this.getGoodsCateInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="totalNum">
					</el-pagination>
				</div>
			</div>
		</el-card>
		<!-- 对话框 -->
		<!-- 添加分类对话框 -->
		<el-dialog @close="addCateDialogClose" title="添加分类" :visible.sync="addCateDialogVisible">
			<el-form label-width="100px" ref="addCateFormRef" :model="addCateForm" :rules="addCateFormRule">
				<el-form-item label="分类名称:" prop="cat_name">
					<el-input v-model="addCateForm.cat_name"></el-input>
				</el-form-item>
				<el-form-item label="父级分类:">
					<el-cascader @change="parentCateChanged" clearable v-model="selectedKeys" :options="parentList" :props="cascaderProps"></el-cascader>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="addCateDialogVisible=false">取 消</el-button>
				<el-button type="primary" @click="postCategories">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data () {
			return {
				// 规则：级联选择器接收数据
				cascaderProps: {
					// lazy: true,
					expandTrigger: 'hover',
					checkStrictly: true,
					label: 'cat_name',
					value: 'cat_id',
					children: 'children'
				},
				// 绑定：级联选择框当前选中项
				selectedKeys: [],
				addCateFormRule: {
					cat_name: [
						{ required: true, message: '请输入分类名称', trigger: 'blur' }
					]
				},
				addCateForm: {
					cat_pid: 0,
					cat_name: '',
					cat_level: 0
				},
				addCateDialogVisible: false,
				currentPage: 1,
				columns: [{
					label: '分类名称',
					prop: 'cat_name'
				},
				{
					label: '是否有效',
					prop: 'cat_deleted',
					type: 'template',
					template: 'isok'
				},
				{
					label: '排序',
					prop: 'cat_level',
					type: 'template',
					template: 'order'
				},
				{
					label: '操作',
					type: 'template',
					template: 'opt'
				}],
					// 参数：请求商品分类数据列表
				getGoodsCateInfo: {
					type: 3,
					pagenum: 1,
					pagesize: 5
				},
				getParentInfo: {
					type: 2
				},
				parentList: [],
				goodsCateList: [],
				totalNum: 0
			}
		},
		methods: {
			// 事件：级联选择器确认按钮被点击
			postCategories () {
				this.$refs.addCateFormRef.validate(async valid => {
					if (!valid) return this.$message.error('请输入分类名称')
					this.addCateDialogVisible = false
					const { data: res } = await this.$http.post('categories', this.addCateForm)
					if (res.meta.status !== 201) return this.$message.error('添加分类失败')
					this.$message.success('添加分类成功')
					// console.log(res)
					this.getGoodsCateList()
				})
				// if (this.addCateForm.cat_name === '') return this.$message.error('请输入分类名称')
			},
			// 事件：级联选择器选中项改变
			parentCateChanged () {
				// console.log(this.selectedKeys)
				if (this.selectedKeys.length === 1) {
					// this.addCateForm.cat_pid = 0
					// this.addCateForm.cat_level = 0
					this.addCateForm.cat_pid = this.selectedKeys[0]
					this.addCateForm.cat_level = 1
				} else if (this.selectedKeys.length === 2) {
					this.addCateForm.cat_pid = this.selectedKeys[1]
					this.addCateForm.cat_level = 2
				}
				console.log(this.addCateForm)
			},
			// 添加分类对话框
			addCateDialogClose () {
				this.$refs.addCateFormRef.resetFields()
				// this.addCateForm.cat_name = ''
				this.selectedKeys = []
				this.addCateForm.cat_pid = 0
				this.addCateForm.cat_level = 0
			},
			handleCurrentChange (newCurrent) {
				this.getGoodsCateInfo.pagenum = newCurrent
				// 重新请求
				this.getGoodsCateList()
			},
			handleSizeChange (newSize) {
				this.getGoodsCateInfo.pagesize = newSize
				// 重新请求
				this.getGoodsCateList()
			},
			// 函数：获取商品分类数据,用于级联选择器
			async getparentList () {
				// this.$message.success('getGoodsCateList')
				const { data: res } = await this.$http.get('categories', { params: this.getParentInfo })
				if (res.meta.status !== 200) {
					return this.$message.error('获取商品分类列表数据失败')
				}
				this.parentList = res.data
				console.log(this.parentList)
			},
				// 函数：获取商品分类数据
			async getGoodsCateList () {
				// this.$message.success('getGoodsCateList')
				const { data: res } = await this.$http.get('categories', { params: this.getGoodsCateInfo })
				if (res.meta.status !== 200) {
					return this.$message.error('获取商品分类列表数据失败')
				}
				this.goodsCateList = res.data.result
				this.totalNum = res.data.total
				console.log(this.goodsCateList)
			},
			addGoodCateBtnClick () {
				// this.$message.success('addGoodCateBtnClick')
				this.getparentList()
				this.addCateDialogVisible = true
			}
		},
		created () {
			this.getGoodsCateList()
		}
	}
</script>

<style lang="less" scoped>
	.el-breadcrumb {
		margin-bottom: 15px;
	}
	.el-card {
		box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15);
	}
	.tree-table {
		margin-top: 15px;
	}
	.el-pagination {
		margin-top: 15px;
	}
	el-cascader{
		width: 100%!important;
	}
</style>
